package com.chuanyekeji.aokai.Controller;

import com.chuanyekeji.aokai.AokaiApplication;
import com.chuanyekeji.aokai.Config.ReadPathConfig;
import com.chuanyekeji.aokai.Entity.CaseImage;
import com.chuanyekeji.aokai.Entity.Cases;
import com.chuanyekeji.aokai.Entity.Model.CaseJsonModel;
import com.chuanyekeji.aokai.Entity.Model.CaseTempModel;
import com.chuanyekeji.aokai.Entity.Seo;
import com.chuanyekeji.aokai.Mapper.CaseImageMapper;
import com.chuanyekeji.aokai.Mapper.CasesMapper;
import com.chuanyekeji.aokai.Mapper.SeoMapper;
import com.chuanyekeji.aokai.Util.DateUtil;
import com.chuanyekeji.aokai.Util.ReturnData;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.*;

@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
public class CasesController {

    @Autowired
    CasesMapper casesMapper;

    @Autowired
    CaseImageMapper caseImageMapper;

    @Autowired
    SeoMapper seoMapper;

    @Autowired
    ReadPathConfig readPathConfig;

    /**
     * 添加案例
     * @param code_mapping
     * @param caseName
     * @param caseName_en
     * @param code_first
     * @param title
     * @param titles
     * @param descs
     * @param keyword
     * @return
     */
    @RequestMapping("addCases")
    String addCases(String code_mapping,String caseName,String caseName_en,String code_first,String title,String titles,String descs,String keyword) {
        Seo seo = new Seo();
        seo.setTitle(titles);
        seo.setDescs(descs);
        seo.setKeyword(keyword);
        seo.setIsFirstPage(0);
        seoMapper.insertSelective(seo);
        String[] images = code_mapping.split(";");
        Cases cases = new Cases();
        cases.setFirstCaseImage(code_first);
        cases.setCaseName(caseName);
        cases.setCaseName_en(caseName_en);
        cases.setCaseName(title);
        cases.setCreatetime(DateUtil.dateTimeToString(new Date()));
        cases.setSeo(seo);
        casesMapper.insertSelective(cases);
        for(String s : images) {
            CaseImage ci = new CaseImage();
            ci.setCases(cases);
            ci.setImageUrl(s);
            ci.setIsEnable(1);
            caseImageMapper.insertSelective(ci);
        }
        return "添加成功";
    }

    /**
     * 获取资讯详情
     * @param id
     * @return
     */
    @RequestMapping("getCases")
    String getCases(int id,HttpSession session) {
        Map<String,Object> data = new HashMap<>();
        Cases cases = casesMapper.selectByPrimaryKey(id);
        Cases nextCase = casesMapper.selectNextId(id);
        Cases lastCase = casesMapper.selectLastId(id);
        int nextId = -1;
        int lastId = -1;
        String nextName = "";
        String lastName = "";

        if(nextCase != null) {
            nextId = nextCase.getId();
            nextName = nextCase.getCaseName();
        }
        if(lastCase != null) {
            lastId = lastCase.getId();
            lastName = lastCase.getCaseName();
        }
        if(!AokaiApplication.language.get(session.getId()).equals("en")) {
            data.put("data",cases);
        } else if(AokaiApplication.language.get(session.getId()).equals("en")) {
            CaseTempModel c = new CaseTempModel();
            c.setCaseName(cases.getCaseName_en());
            c.setCreatetime(cases.getCreatetime());
            c.setFirstCaseImage(cases.getFirstCaseImage());
            c.setSeo(cases.getSeo());
            c.setId(cases.getId());
            data.put("data",cases);
        }
        data.put("nextPage",nextId);
        data.put("nextName",nextName);
        data.put("lastPage",lastId);
        data.put("lastName",lastName);
        return ReturnData.success("success",new Gson().toJson(data));
    }

    /**
     * 获取资讯列表
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("getCasesList")
    String getCasesList(@RequestParam(required=false,defaultValue="1") int page, @RequestParam(required=false,defaultValue="10") int rows,
                        HttpSession session
    ) {
        int lastRow = rows * page;
        int indexRow = lastRow - rows;
        List<Cases> casesList = casesMapper.getCases(indexRow,lastRow);
        List<CaseJsonModel> caseJsonModelList = new ArrayList<>();
        for(Cases c : casesList) {
            CaseJsonModel model = new CaseJsonModel();
            model.setId(c.getId());
            model.setCaseName(c.getCaseName());
            model.setCaseName_en(c.getCaseName_en());
            model.setSeoTitle(c.getSeo().getTitle());
            model.setSeoDescs(c.getSeo().getDescs());
            model.setSeoKeyword(c.getSeo().getKeyword());
            model.setCreatetime(c.getCreatetime());
            caseJsonModelList.add(model);
        }

        Map<String,Object> data = new HashMap<>();
        data.put("total",casesMapper.getCasesCount());
        data.put("rows",caseJsonModelList);
        return new Gson().toJson(data);
    }

    /**
     * 获取资讯列表
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("getCasesListByWeb")
    String getCasesListByWeb(@RequestParam(required=false,defaultValue="1") int page, @RequestParam(required=false,defaultValue="10") int rows) {
        int lastRow = rows * page;
        int indexRow = lastRow - rows;
        List<Cases> casesList = casesMapper.getCases(indexRow,lastRow);
        List<CaseJsonModel> caseJsonModelList = new ArrayList<>();
        for(Cases c : casesList) {
            CaseJsonModel model = new CaseJsonModel();
            model.setId(c.getId());
            model.setCaseName(c.getCaseName());
            model.setCaseName_en(c.getCaseName_en());
            model.setFirstCaseImage(c.getFirstCaseImage());
            model.setSeoTitle(c.getSeo().getTitle());
            model.setSeoDescs(c.getSeo().getDescs());
            model.setSeoKeyword(c.getSeo().getKeyword());
            model.setCreatetime(c.getCreatetime());
            caseJsonModelList.add(model);
        }
        Map<String,Object> data = new HashMap<>();
        data.put("total",casesMapper.getCasesCount());
        data.put("rows",caseJsonModelList);
        return ReturnData.success("success",data);
    }

    /**
     * 删除资讯
     * @param id
     * @return
     */
    @RequestMapping("delCases")
    String delCases(int id){
        casesMapper.deleteByPrimaryKey(id);
        return ReturnData.success("删除成功");
    }

    /**
     * 修改案例SEO
     * @param id
     * @param caseName
     * @param seoTitle
     * @param seoKeyword
     * @param seoDescs
     * @return
     */
    @RequestMapping("changeCasesSeo")
    String changeCasesSeo(int id,String caseName,String caseName_en,String seoTitle,String seoKeyword,String seoDescs) {
        Cases cases = casesMapper.selectByPrimaryKey(id);
        if(caseName != null || caseName_en != null) {
            cases.setCaseName(caseName);
            cases.setCaseName_en(caseName_en);
            casesMapper.updateByPrimaryKeySelective(cases);
        }
        Seo s = cases.getSeo();
        Seo seo = new Seo();
        seo.setId(s.getId());
        seo.setTitle(seoTitle);
        seo.setKeyword(seoKeyword);
        seo.setDescs(seoDescs);
        seoMapper.updateByPrimaryKeySelective(seo);
        return ReturnData.success("修改成功");
    }

}
